//
//  ServiceRegulationsViewController.m
//  YuanCheng
//
//  Created by 朱亚杰 on 16/3/23.
//  Copyright © 2016年 dongshangtong. All rights reserved.
//

#import "ServiceRegulationsViewController.h"
#import "YCCommonTool.h"

@interface ServiceRegulationsViewController ()

@property (nonatomic, weak) UIScrollView *scrollView;

@end

@implementation ServiceRegulationsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

#pragma mark 设置UI
- (void)setupUI{
    
    //    1. 设置navigation
    self.title = @"服务条例";
    self.view.backgroundColor = BackgroundColor;

    CGFloat scrollHeight = SCREEN_HEIGHT - 10 - 64;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, scrollHeight)];
    scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView = scrollView;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 15, 100, 26)];
    label.text = @"服务条例";
    label.textColor = GreenColor;
    [scrollView addSubview:label];
    
    //    2. 版权说明
    NSString *str = @"本网站所使用的www.qzrs.gov.cn、泉州人事网以及本网站的其他注册域名、商标标识及专有名称均受相关法律保护，未经泉州市公务员局的同意，任何单位及个人均不得将上述域名、商标及专有名称进行容易引起混淆或歧义的使用。本网站所使用的网站标识、图案、中英文名称为泉州市公务员局政府门户网站专有标识，未经网站所有者许可，任何人不得使用。";
    CGFloat illustrateY = CGRectGetMaxY(label.frame) + 15;
    UIView *copyrightIllustrate = [self viewWithTitle:@"版权说明" detailStr:str labelY:illustrateY lastView:0];
    
    //    3. 版权
    NSString *copyRigthStr = @"限于：文本、图形图片、视像及声音内容、LOGO标识，版面设计，专栏目录与名称、内容分类）的所有权归泉州市公务员局所有。 本网站的内容和软件均受《中华人民共和国著作权法》及其它相关法律的保护。任何单位或个人将本网站提供的内容与服务用于商业、盈利、广告性等目的时，需征得泉州市公务员局相关权利人的书面许可；将本网法律的规定，不得侵犯网站所有者及相关权利人的权益。";
    CGFloat copyRightY = CGRectGetMaxY(copyrightIllustrate.frame) + 15;
    UIView *copyright = [self viewWithTitle:@"版权" detailStr:copyRigthStr labelY:copyRightY lastView:0];
    
    //    4. 版权异议
    NSString *copyrightDisputeStr = @"如权利人认为本网站内容中所涉及的文字作品、图片和音视频资料（以下简称“作品”），侵犯其著作权的请及时书面通知本网站，本网站将依法删除被指侵权的作品或断开相应的链接；但权利人不能出示有效身份证明。";
    CGFloat copyrightDisputeY = CGRectGetMaxY(copyright.frame) + 15;
    UIView *copyrightDispute = [self viewWithTitle:@"版权异议" detailStr:copyrightDisputeStr labelY:copyrightDisputeY lastView:1];
    
    CGFloat height = CGRectGetMaxY(copyrightDispute.frame) + 10;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, height);
    scrollView.scrollEnabled = YES;
    [self.view addSubview:scrollView];
}

#pragma mark 通过标题，内容，来设置label和view
- (UIView *)viewWithTitle:(NSString *)title detailStr:(NSString *)detailStr labelY:(CGFloat)labelY lastView:(NSInteger)lastView {
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, labelY, 100, 15)];
    titleLabel.text = title;
    titleLabel.font = SubTitleFont;
    [_scrollView addSubview:titleLabel];
    
    CGFloat detailY = CGRectGetMaxY(titleLabel.frame) + 10;
    CGFloat detailHeight = [self heightWithStr:detailStr lineSpacing:5 fontSize:14.0 maxLabelWidth:kScreenWidth - 24 firstLineIndent:20];
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, detailY, kScreenWidth - 24, detailHeight)];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:detailStr];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.firstLineHeadIndent = 30;
    style.lineSpacing = 5;
    [attrStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attrStr.length)];
    detailLabel.attributedText = attrStr;
    detailLabel.numberOfLines = 0;
    detailLabel.textColor = RGBACOLOR(102, 102, 102, 1);
    detailLabel.font = [UIFont systemFontOfSize:14.0];
    [_scrollView addSubview:detailLabel];
    
    if (lastView == 0) {
        
        CGFloat dividerY = CGRectGetMaxY(detailLabel.frame) + 15;
        UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(0, dividerY, SCREEN_WIDTH, 1)];
        divider.backgroundColor = RGBACOLOR(214, 214, 214, 1);
        [_scrollView addSubview:divider];
        return divider;
    }else {
        
        CGFloat dividerY = CGRectGetMaxY(detailLabel.frame);
        UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(10, dividerY, kScreenWidth - 24, 10)];
        divider.backgroundColor = [UIColor clearColor];
        [_scrollView addSubview:divider];
        return divider;
    }
}

#pragma mark 根据文字，间距，字体大小，最大宽度，首行缩进 来 获取label的高度
- (CGFloat) heightWithStr:(NSString *)str lineSpacing:(CGFloat)lineSpacing fontSize:(CGFloat)size maxLabelWidth:(CGFloat)MaxLabelWidth firstLineIndent:(CGFloat)firstLineIndent{
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    
    if (lineSpacing != 0) {
        style.lineSpacing = lineSpacing;
        style.firstLineHeadIndent = firstLineIndent;
        [attrStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attrStr.length)];
    }
    
    //    3.2. 计算文字的高度，根据 label 的宽最大度和行间距
    NSDictionary *dictonary = @{
                                NSFontAttributeName : [UIFont systemFontOfSize:size],
                                NSParagraphStyleAttributeName : style
                                };
    
    CGFloat height = [str boundingRectWithSize:CGSizeMake(kScreenWidth - 24, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictonary context:nil].size.height;
    return height;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
